import javax.swing.*;
import java.awt.*;
/**
 * La clase MachineOperation agrupa los datos que describen el 
 * modo y estado de operación de la máquina.
 */
public class MachineOperation extends DataCategory
{
    public MachineOperation()
    {
    }
    public boolean initData(int id,int value)
    {
        switch(id)
        {
            case KbdCmdId.CmdId.EMERGENCY:
                emergencyStop = Integer.valueOf(value);
                return true;
            case KbdCmdId.CmdId.MODE:
                operationMode = Integer.valueOf(value);
                return true;
            default:
                return false;
        }
    }
    public boolean initData(int id,long value)
    {
        return initData(id,(int) value);
    }
    public Component getView(Id viewID)
    {
        JPanel panel = new JPanel();
        switch(viewID)
        {
            case EMERGENCY_STOP:
                JLabel emergencyStopLabel = 
                    NewComp.getLabel(
                        "emergencyStop", 
                        "titleTop", 
                        "red"
                    );
                emergencyStopIcon.setIcon(
                    NewComp.getIcon(emergencyStopOpt[emergencyStop]));
                panel.add(emergencyStopLabel);
                panel.add(emergencyStopIcon);
                return panel;
            case OPERATION_MODE:
                panel.setLayout(new BorderLayout(16,16));
                oprtnModeLabel.setText(
                    NewComp.getStringWithFormat(
                        operationModeOpt[operationMode],
                        "titleTop", 
                        "black"
                    )
                );
                oprtnModeLabel.setOpaque(true);
                oprtnModeLabel.setBackground(Color.orange);
                panel.add(oprtnModeLabel,BorderLayout.CENTER);
                return panel;
            default:
                return null;
        }
    }
    public Component getEditor(Id editorID)
    {
        return null;
    }
    public String getDataCategoryID()
    {
        return "machineOperation";
    }
    public String getValue(Id dataID)
    {   
        switch(dataID)
        {
            case OPERATION_MODE:
                return operationModeOpt[operationMode];
            default:
                return null;
        }
    }
    public boolean setCommandValue(int id,int value)
    {
        if(!initData( id, value))
            return false;
        if(id == KbdCmdId.CmdId.EMERGENCY)
        {
            emergencyStopIcon.setIcon(
                NewComp.getIcon(emergencyStopOpt[emergencyStop])
            );
            return true;
        }
        else
            return false;
        
    }
    public boolean setCommandValue(int id,long value)
    {
        if(!initData( id, value))
            return false;
        if(id == KbdCmdId.CmdId.EMERGENCY)
        {
            emergencyStopIcon.setIcon(
                NewComp.getIcon(emergencyStopOpt[emergencyStop])
            );
            return true;
        }
        else
            return false;
        
    }
    public boolean setDataValue(int id,int value)
    {
        return false;
    }
    public boolean setDataValue(int id,long value)
    {
        return false;
    }
    public void applyChange()
    {
        while( changed.isEmpty() == false)
        {
            switch(changed.get(0))
            {
                case OTHER:
                    break;
                default:
                    break;
            }
            changed.remove(0);
        }
    }
    public void cancelChange()
    {
        while( changed.isEmpty() == false)
        {
            switch(changed.get(0))
            {
                case OTHER:
                    break;
                default:
                    break;
            }
        }
        changed.clear();
    }
    /**
     * Proporciona el modo de operación en uso.
     * @return una cadena de texto con el nombre clave del
     * modo de operación en uso. Los valores son: "memoryMode",
     * "mdiMode", "fileManagerMode", "editorMode", "handwheelMode", 
     * "jogMode", "rapidMode" y "zeroReturnMode"
     */
    public static String getOperationMode()
    {
        return operationModeOpt[operationMode];
    }
    static
    {
        emergencyStop = new Integer(0);
        operationMode = new Integer(0);
        oprtnModeLabel = NewComp.getLabel("default","titleTop","black");
        emergencyStopIcon = NewComp.getLabel();
    }
    private static final String[] emergencyStopOpt =
    {
        "stop",
        "continue"
    };
    private static final String[] operationModeOpt =
    {
         "memoryMode",
         "mdiMode",
         "fileManagerMode",
         "editorMode",
         "handwheelMode",
         "jogMode",
         "rapidMode",
         "zeroReturnMode"
    };
    private static Integer emergencyStop;
    private static Integer operationMode;
    private static JLabel oprtnModeLabel;
    private static JLabel emergencyStopIcon;
}
